package cn.com.nes.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.Random;

import static cn.com.nes.common.util.MapSendPostUtils.sendPost;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ShenWei
 * @Date: 2024/03/08/16:10
 */
public class MapUtils {

    //    private static String KEY_ = "5567ee0fdcdedc59690d132b5d89fae6";
    private static String KEY_ = "4a426fb6dd527fccb997391577d88e48";

    private static final ArrayList<String> elevationKey = new ArrayList<String>() {{
        add("UZb0GShXqFizBKMWgEq5LDlU5WT7aBJ2");
        add("nqJeCXuGBBRyQValMllij9dh5QozJ0oX");
        add("zJpb9Bpr0ZKKnZhqfWvxoxj9hKKB6Sld");
        add("49xaGKWAXWNQGG3SGiHc1G59Ey45rLBt");
        add("qO5HBuGkpGCJZmyRkErbNK2yj5gF8pUh");
        add("ukod0Gz3hFtCy0gQoDnhEVw80wEewGVb");
        add("UEQG5WFI9hb68vEfpfHIfIA8UOIN1DQP");
        add("Rnodo0GTN0IK8fpaVlRuTh3H0vX7yX6T");
        add("APJbjywSbr9c1OMrnQGV75mzr61rd5MG");
        add("akEy8x7AkVmsEoa18GUcAccaNT85GcpN");
        add("ljA2ixNy0JufkQNV2It9cIb8dljyrtTh");
    }};

    /**
     * 阿里云api 根据经纬度获取地址
     *
     * @param log
     * @param lat
     * @return
     */
    public static String getCity(String log, String lat) {
        StringBuffer s = new StringBuffer();
        s.append("key=").append(KEY_).append("&location=").append(log)
                .append(",").append(lat);
        String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
        JSONObject jsonObject = JSONObject.parseObject(res);
        String regeocode = jsonObject.getString("regeocode");
        JSONObject jsonObject2 = JSONObject.parseObject(regeocode);
        String add = jsonObject2.getString("formatted_address");
        return add;
    }

    /**
     * 阿里云api 根据经纬度获取地址
     *
     * @param log
     * @param lat
     * @return
     */
    public static JSONObject getCityName(String log, String lat) {
        if (StrUtil.isBlank(log) || StrUtil.isBlank(lat)) {
            return null;
        }
        if (Double.parseDouble(log) == 0 || Double.parseDouble(lat) == 0) {
            return null;
        }
        StringBuffer s = new StringBuffer();
        s.append("key=").append(KEY_).append("&location=").append(log)
                .append(",").append(lat);
        String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
        JSONObject jsonObject = JSONObject.parseObject(res);
        String status = jsonObject.getString("status");
        if ("1".equals(status)) {
            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
            return regeocode.getJSONObject("addressComponent");
        }
        return null;
    }

    public static JSONObject getGeoByAddress(String address) {
        return getGeoByAddress(address, "");
    }
    public static JSONObject getGeoByAddress(String address, String code) {
        StringBuffer s = new StringBuffer();
        s.append("key=").append(KEY_).append("&address=").append(address).append("&city=").append(code);
        String res = sendPost("https://restapi.amap.com/v3/geocode/geo", s.toString());
        JSONObject jsonObject = JSONObject.parseObject(res);
        String status = jsonObject.getString("status");
        System.out.println(jsonObject);
        if ("1".equals(status)) {
            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            if (geocodes != null && geocodes.size() > 0) {
                JSONObject geocode = geocodes.getJSONObject(0);
                String adcode = geocode.getString("adcode");
                adcode = StrUtil.fill(adcode, '0', 12, false);
                geocode.put("adcode", adcode);
                String location = geocode.getString("location");
                String[] lngLat = location.split(",");
                String lng = "";
                String lat = "";
                if (lngLat != null && lngLat.length == 2) {
                    lng = lngLat[0];
                    lat = lngLat[1];
                }
                geocode.put("lng", lng);
                geocode.put("lat", lat);
                return geocode;
            }
        }
        return null;
    }

    public static Integer getDistanceBetweenOriginNDestination(String originLat, String originLng,
                                                               String destinationLat, String destinationLng) {
        StringBuffer s = new StringBuffer();
        if (StrUtil.hasBlank(originLat, originLng, destinationLat, destinationLng)) {
            return null;
        }
        s.append("key=").append(KEY_)
                .append("&origin=").append(originLng).append(",").append(originLat)
                .append("&destination=").append(destinationLng).append(",").append(destinationLat);
        String res = sendPost("https://restapi.amap.com/v5/direction/driving", s.toString());
        JSONObject jsonObject = JSONObject.parseObject(res);
        String status = jsonObject.getString("status");
        if ("1".equals(status)) {
            JSONObject route = jsonObject.getJSONObject("route");
            JSONArray paths = route.getJSONArray("path");
            if (ObjectUtil.isNull(paths)) {
                paths = route.getJSONArray("paths");
            }
            JSONObject path = paths.getJSONObject(0);
            String distance = path.getString("distance");
            return StrUtil.isNotBlank(distance) ? Integer.parseInt(distance) : null;
        }
        return null;
    }

    public static String getElevationKey() {
        int random = new Random().nextInt();
        int size = elevationKey.size();
        return elevationKey.get(Math.abs(random % size));
    }

    public static void main(String[] args) {
//        final Integer distanceBetweenOriginNDestination = getDistanceBetweenOriginNDestination( "40.40","115.98",
//                 "39", "115.36");
//        System.out.println(distanceBetweenOriginNDestination);
//        JSONObject geoByAddress = getGeoByAddress("河北省承德市双滦区幸福街靠近侯家菜园铜锅涮", "");
//        System.out.println(geoByAddress);
        double jd = 116.371381;
        double wd = 40.391908;
        for (int i = 0; i < 11; i++) {
            final double lng = jd + Math.pow(-1, new Random().nextInt()) * ((double) new Random().nextInt(200) / 1000000);
            final double lat = wd + Math.pow(-1, new Random().nextInt()) * ((double) new Random().nextInt(200) / 1000000);
            System.out.println(lng + "," + lat);
        }
    }
}
